//
//  CNUIViewController.swift
//  YiZhangGui
//
//  Created by 卢逢卫 on 2024/4/7.
//

import UIKit

extension UIViewController {
    /// CN_BaseController 的 sheet方式跳转
    public func cn_presentWithSheet(_ alertCon: CN_BaseController, statusBarDarkMode: Bool, completion: (() -> Void)? = nil) {
//        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
//        alertController.setValue(alertCon, forKey: "contentViewController")
//        present(alertController, animated: true, completion: completion)
//        setStatusBarStyle(statusBarDarkMode)
        self.present(alertCon, animated: true)
    }

    /// UIViewController 的 sheet方式跳转
    public func cn_presentWithSheet(_ alertCon: UIViewController, statusBarDarkMode: Bool, completion: (() -> Void)? = nil) {
//        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
//        alertController.setValue(alertCon, forKey: "contentViewController")
//        present(alertController, animated: true, completion: completion)
//        setStatusBarStyle(statusBarDarkMode)
        self.present(alertCon, animated: true)
    }

    /// CN_BaseController 的 alert方式跳转
    public func cn_presentWithAlert(_ alertCon: CN_BaseController, statusBarDarkMode: Bool, completion: (() -> Void)? = nil) {
//        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
//        alertController.setValue(alertCon, forKey: "contentViewController")
//        present(alertController, animated: true, completion: completion)
//        setStatusBarStyle(statusBarDarkMode)
        self.present(alertCon, animated: true)
    }

    /// UIViewController 的 alert方式跳转
    public func cn_presentWithAlert(_ alertCon: UIViewController, statusBarDarkMode: Bool, completion: (() -> Void)? = nil) {
//        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
//        alertController.setValue(alertCon, forKey: "contentViewController")
//        present(alertController, animated: true, completion: completion)
//        setStatusBarStyle(statusBarDarkMode)
        self.present(alertCon, animated: true)
    }

    private func setStatusBarStyle(_ statusBarDarkMode: Bool) {
//        if statusBarDarkMode {
//            UIApplication.shared.statusBarStyle = .default
//        } else {
//            UIApplication.shared.statusBarStyle = .lightContent
//        }
    }
}

extension UIViewController {
    /// CN_BaseController 的 push方式跳转
    public func cn_pushController(controller: CN_BaseController, hideNavigationBar: Bool, statusBarDarkMode: Bool, animated: Bool = true) {
        if let navigationController = navigationController {
            navigationController.pushViewController(controller, animated: animated)
//            setNavigationBarHidden(navigationController, hideNavigationBar)
            controller.cn_hideNavigationBar = hideNavigationBar
            
            setStatusBarStyle(statusBarDarkMode)
        }
    }

    /// UIViewController 的 push方式跳转
    public func cn_pushController(controller: UIViewController, hideNavigationBar: Bool, statusBarDarkMode: Bool, animated: Bool = true) {
        if let navigationController = navigationController {
            
            navigationController.pushViewController(controller, animated: animated)
//            setNavigationBarHidden(navigationController, hideNavigationBar)
            controller.navigationController?.setNavigationBarHidden(hideNavigationBar, animated: false)
            setStatusBarStyle(statusBarDarkMode)
            
        }
    }

    private func setNavigationBarHidden(_ navigationController: UINavigationController, _ hideNavigationBar: Bool) {
        navigationController.setNavigationBarHidden(hideNavigationBar, animated: false)
    }

//    private func setStatusBarStyle(_ statusBarDarkMode: Bool) {
//        if statusBarDarkMode {
//            UIApplication.shared.statusBarStyle = .default
//        } else {
//            UIApplication.shared.statusBarStyle = .lightContent
//        }
//    }
}
